<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/601568 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="527"/>
<h1>04-函数（上）</h1>
<div>
<table bgcolor="#D4DDE5" border="0">
<tr><td><b>创建时间：</b></td><td><i>2018/4/4 15:51</i></td></tr>
<tr><td><b>更新时间：</b></td><td><i>2020/4/2 16:39</i></td></tr>
<tr><td><b>作者：</b></td><td><i>18081003141@163.com</i></td></tr>
</table>
</div>
<br/>

<div>
<span><div><div><span style="font-size: 14pt;">奥特曼打小怪兽</span></div><div><br clear="none"/></div><div><br/></div><div><br clear="none"/></div><div>                                                       <img src="00-笔记_files/奥特曼.gif" type="image/gif" data-filename="奥特曼.gif" width="315"/></div><div><br/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">大招是怎么发出来的？我们只知道奥特曼在被打的快趴下的时候一般就可以发大招制服小怪兽了</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    那么，发大招这个技能是被奥特曼自己封装起来的，在某一特定条件下就会触发这个行为，而且这个行为能够反复发生，需要的时候执行一下就可以达到相同的发大招效果</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    其实，这很类似于js，对一些功能进行封装包裹，在需要的时候只需要再将封装好的行为拿出来再执行一遍就好了。</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt;"><span style="font-size: 14pt; font-weight: bold;">函数：一个工具，被封装好可重复执行的一段代码块</span></span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    把某一段代码放在函数里面进行封装，</span><span style="font-size: 16px;">可以避免在非必要情况下调用该代码，也就是说我们可以让一段代码在特定情况下再去执行，而且每次需要执行这一段代码的时候只需调用一下这个函数即可（函数名）</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">函数就像拖拉机一样，制造一台拖拉机可以反复开，只要有钥匙，就可以一键启动</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;"><span style="font-size: 12pt;"><img src="00-笔记_files/拖拉机.jpg" type="image/jpeg" data-filename="拖拉机.jpg" style="font-size: 16pt;" width="610"/></span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">我们在需要的时候可以调用一个封装好的函数，函数内的一些代码就会被执行，功能就会被实现</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt;">系统函数： parseInt()   alert()  prompt()  eval()</span></div><div><br clear="none" style="font-size: 16pt;"/></div><div><span style="font-size: 14pt;">自定义函数：自己封装一段代码，需要的时候再调用</span></div><div><br clear="none" style="font-size: 16pt;"/></div><div><span style="font-size: 14pt;">函数的声明和使用</span></div><div><br clear="none" style="font-size: 16pt;"/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 12pt; color: #95a3ab; font-family: consolas;">//通过function关键字声明一个函数，跟上函数名，一对小括号，一对花括号，花括号里面放代码块</span></div><div><span style="font-size: 12pt; min-height: 26pt; font-family: consolas; color: #364bc0;">function</span> <span style="font-size: 12pt; min-height: 26pt; font-family: consolas; color: #db7800;">test</span><span style="font-size: 12pt; min-height: 26pt; font-family: consolas; color: #080808;">(){</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #95a3ab; font-size: 12pt;">    //可重复执行的代码块</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">}</span><span style="min-height: 26pt; font-size: 12pt; color: #080808; font-family: consolas;">  </span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">test();</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">test();</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">test();</span></div><div style="min-height: 26pt;"><div style="font-size: 16pt;"><br clear="none" style="font-size: 16pt;"/></div></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; font-size: 12pt; color: #a8a8a8;">//表达式定义法</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #364bc0; font-size: 12pt;">var </span><span style="min-height: 26pt; font-family: consolas; color: #db7800; font-size: 12pt;">test1 =</span> <span style="min-height: 26pt; font-family: consolas; color: #364bc0; font-size: 12pt;">function</span><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">(){</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #95a3ab; font-size: 12pt;">    </span><span style="min-height: 26pt; font-family: consolas; color: #95a3ab; font-size: 12pt;">//可重复执行的代码块</span></div><div style="min-height: 26pt;"><div style="font-size: 16pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">}</span></div></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #577909; font-size: 12pt;">for</span> <span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">(</span><span style="min-height: 26pt; font-family: consolas; color: #364bc0; font-size: 12pt;">var</span> <span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">i</span> <span style="min-height: 26pt; font-family: consolas; color: #577909; font-size: 12pt;">=</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb; font-size: 12pt;">0</span><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">; i</span> <span style="min-height: 26pt; font-family: consolas; color: #577909; font-size: 12pt;">&lt;</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb; font-size: 12pt;">10</span><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">; i</span><span style="min-height: 26pt; font-family: consolas; color: #577909; font-size: 12pt;">++</span><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">) {</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-size: 12pt; font-family: consolas; color: #3c7a03;">    </span><span style="min-height: 26pt; font-size: 12pt; font-family: consolas; color: #080808;">test1();</span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">}</span></div></div><div><br clear="none"/></div><div><br/></div><div><br/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt; font-weight: bold;">案例：打印三行四列的表格</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt;">函数参数</span></div><div><span style="font-size: 12pt;">    形参：形式参数，</span><span style="font-size: 12pt;">声明</span><span style="font-size: 12pt;">函数的时候写在小括号里面的参数，无需var</span></div><div><span style="font-size: 12pt;">    实参：实际参数，在函数调用的时候需要传递实际有值得参数</span></div><div><span style="font-size: 12pt;">    </span></div><div><span style="font-size: 12pt;">    实参个数大于形参，多余实参自动舍弃</span></div><div><span style="font-size: 12pt;">    形参个数大于实参，多余形参默认为undefined</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    实参副本arguments：可以在函数内部使用arguments接收所传递过来的参数，是一个集合</span></div><div><span style="font-size: 12pt;">     </span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #364bc0;">function</span> <span style="min-height: 26pt; font-family: consolas; color: #db7800;">test</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">(){</span></span></div><div style="min-height: 12pt;"><span style="font-size: 12pt;"><span style="min-height: 12pt; font-family: consolas; color: #3c7a03;">    console</span><span style="min-height: 12pt; font-family: consolas; color: #080808;">.log(</span><span style="min-height: 12pt; font-family: consolas; color: #3c7a03;">arguments<span style="font-size: 12pt; min-height: 12pt; font-family: consolas; color: #080808;">)<span style="font-size: 12pt; min-height: 12pt; font-family: consolas; color: #080808;">;</span></span></span></span></div><div style="min-height: 12pt;"><span style="font-size: 12pt;"><span style="min-height: 12pt; font-family: consolas; color: #080808;"><span style="min-height: 26pt; font-family: consolas; color: #3c7a03; font-size: 12pt;"><span style="min-height: 12pt; font-family: consolas; color: #3c7a03; font-size: 12pt;">    console</span><span style="min-height: 12pt; font-family: consolas; color: #080808; font-size: 12pt;">.log(</span>arguments</span><span style="min-height: 26pt; font-family: consolas; color: #080808; font-size: 12pt;">.length</span>);</span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;">}</span><span style="color: #080808; font-family: consolas;">        </span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;">test(</span><span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">1</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">,</span><span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">2</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">,</span><span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">3</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">,</span><span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">5</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">,</span><span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">6</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">)</span> <span style="min-height: 26pt; font-family: consolas; color: #95a3ab;">// 打印 1,2,3,5,6   长度为5</span></span></div></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt; font-weight: bold;">练习</span></div><div><br clear="none"/></div><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">    1、编写一个函数，计算n ~ m之间的闰年（传参实现）</span></span></div><div><span style="font-size: 12pt;">    2、函数实现1-100中7的倍数或者带7的数（逢任何数字（通过传参）过）</span></div><div><span style="font-size: 12pt; text-indent: 0pt; color: rgb(1, 1, 1);">    3、编写函数判断任意一个数是否是素数</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt;"><span style="font-size: 16pt; font-weight: bold;">作用域</span></span></div><div><br clear="none"/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font face="等线" style="font-size: 14pt;"><span style="color: #000000;"><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">function fun()</span></span><span style="color: rgb(0, 0, 0);">{</span></font></div><div><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="color: #000000;"><font face="等线" style="font-size: 14pt;">    var a =0;</font></span></span></div><div><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="color: #000000;"><font face="等线" style="font-size: 14pt;">    alert(a);  </font></span></span></div><div><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="color: #000000;"><font face="等线" style="font-size: 14pt;">}</font></span></span></div><div><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="color: #000000;"><font face="等线" style="font-size: 14pt;">fun();</font></span></span></div><div><span style="font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="color: rgb(0, 0, 0);"><font face="等线" style="font-size: 14pt;">alert(a);</font></span></span></div></div><div><br clear="none"/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 12pt; font-family: Consolas;">作用域：变量的作用范围</span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">全局变量</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    作用范围为整个程序的执行范围</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    在函数体外部定义的变量就是全局变量</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    在函数体内部不使用var定义的也是全局变量</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">局部变量</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    作用范围是某个函数体内部</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    在函数体内部通过var关键字定义的变量或者形参，都是局部变量</span></span></div><div><span style="font-size: 12pt; color: #333333;"><span style="font-family: Consolas;">    当局部变量与全局变量重名时，在函数体内部局部变量优先于全局变量</span></span></div><div><br clear="none"/></div></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br/></div><div><span style="font-size: 14pt;">局部变量如何跟全局变量之间如何发生联系？</span></div><div><img src="00-笔记_files/猪肉.jpg" type="image/jpeg" data-filename="猪肉.jpg" style="font-size: 14pt;" width="369"/>  <img src="00-笔记_files/香肠机.jpg" type="image/jpeg" data-filename="香肠机.jpg" width="330"/>     <img src="00-笔记_files/香肠.jpg" type="image/jpeg" data-filename="香肠.jpg" width="367"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt; font-weight: bold;">return</span><span style="font-size: 14pt;">关键字</span></div><div><span style="font-size: 14pt;">   </span> <span style="font-size: 12pt;">1.结束函数的执行</span></div><div><span style="font-size: 12pt;">     2.交回函数执行权</span></div><div><span style="font-size: 12pt;">     3.返回一个结果函数调用位置</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt;">堆栈</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    </span><span style="font-size: 12pt;">是一种数据结构，指的是数据存取的方式，当定义一个变量时，内存会开辟一段空间</span></div><div><span style="font-size: 12pt;">    </span></div><div><span style="font-size: 12pt;">    栈（</span> <span style="font-size: 12pt; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(73, 73, 73); font-variant-caps: normal; font-variant-ligatures: normal;">Stack</span><span style="font-size: 12pt;">）：先进后出（FILO）</span><span style="font-size: 12pt;">，</span><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(73, 73, 73); font-variant-caps: normal; font-variant-ligatures: normal;">在栈顶做插入（压栈）和删除操作（出栈）。</span></div><div><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt;">    </span></div><div><span style="font-size: 12pt; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; widows: 2; word-spacing: 0px;"> </span></div><div><br clear="none"/></div><div><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(73, 73, 73); font-variant-caps: normal; font-variant-ligatures: normal;">                    <img src="00-笔记_files/背篓.jpg" type="image/jpeg" data-filename="背篓.jpg" style="font-size: 12pt; color: #494949;" width="415"/> </span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    队列：先进先出（FIFO）</span><span style="font-size: 12pt;">，</span><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(73, 73, 73); font-variant-caps: normal; font-variant-ligatures: normal;">在队头做删除操作,在队尾做插入操作。</span></div><div><br clear="none"/></div><div>                          <img src="00-笔记_files/排队.jpg" type="image/jpeg" data-filename="排队.jpg" style="font-size: 12pt; color: #494949;" width="400"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 12pt; color: rgb(73, 73, 73); font-variant-caps: normal; font-variant-ligatures: normal;">    堆和它们不同，代码执行时系统动态分配，不存在是先进后出还是先进先出。</span></span></div><div><span style="font-size: 12pt;">    </span></div><div><span style="font-size: 16px;">        </span></div><div><br clear="none"/></div><div><br clear="none"/></div><h2 style="margin: 8px 0px 16px; padding: 0px; box-sizing: border-box; text-align: start; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff;"><span style="box-sizing: border-box; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 14pt; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; line-height: 32px;">执行环境执行栈（也称执行上下文–execution context）</span></span></h2><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal;">    当JavaScript解释器初始化执行代码时，它首先默认进入全局执行环境，从此刻开始，</span><span style="box-sizing: border-box; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">函数的每次调用都会创建一个新的执行环境，<span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">每一个执行环境都会创建一个新的环境对象压入栈中</span></span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal;">。</span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal;"> </span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal;">    当执行流进入一个函数时，函数的环境对象就会被压入一个环境栈中（execution stack）。在函数执行完后，栈将其环境弹出，把控制权返回给之前的执行环境。</span></div><div>                                                            <img src="00-笔记_files/执行环境.jpg" type="image/jpeg" data-filename="执行环境.jpg" width="544"/></div><div><br clear="none"/></div><div><span style="font-size: 14pt;">作用域链</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">    </span> <span style="box-sizing: border-box; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(79, 79, 79); font-variant-caps: normal; font-variant-ligatures: normal;">内层环境可以访问外层中的变量和函数，而外层环境不能访问内层的变量和函数</span></div><div><span style="box-sizing: border-box; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><h3 style="margin: 10px 0px; padding: 0px; list-style-type: none; list-style-image: none; font-size: 16px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(68, 68, 68); font-family: Tahoma, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 1.5;">变量生命周期</span></h3><div><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(68, 68, 68); font-variant-caps: normal; font-variant-ligatures: normal;">全局变量的生命周期直至浏览器卸载页面才会结束。</span></div><div><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(68, 68, 68); font-variant-caps: normal; font-variant-ligatures: normal;">局部变量只在函数的执行过程中存在，而在这个过程中会为局部变量在栈或堆上分配相应的空间，以存储它们的值，然后再函数中使用这些变量，直至函数结束</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br/></div><div><br clear="none"/></div><div><span style="font-size: 14pt;">汉诺塔问题</span></div><div><br clear="none"/></div><div>         <img src="00-笔记_files/汉诺塔.png" type="image/png" data-filename="汉诺塔.png" style="font-size: 14pt;" width="702"/></div><div><span style="font-size: 14pt;">   </span></div><div><br clear="none"/></div><div><span style="font-size: 14pt;"> </span></div><div><span style="font-size: 18.6667px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">递归</span></div><div><br clear="none" style="font-size: 12pt;"/></div><div><span style="letter-spacing: normal; orphans: 2; text-indent: 28px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(54, 101, 238); font-variant-caps: normal; font-variant-ligatures: normal;">    程序调用自身的编程技巧称为递归（ recursion）。</span></div><div><br clear="none" style="font-size: 12pt;"/></div><div><span style="text-indent: 2em; font-size: 12pt; color: rgb(51, 51, 51);">递归，就是在运行的过程中调用自己，本质就是循环。</span></div><div><br clear="none" style="font-size: 12pt;"/></div><div><span style="font-size: 12pt; text-indent: 2em; color: rgb(51, 51, 51);">构成递归需具备的条件</span></div><div><span style="font-size: 12pt; text-indent: 2em; color: rgb(51, 51, 51);">    1. 子问题须与原始问题为同样的事，且更为简单；</span></div><div><span style="word-wrap: break-word; text-indent: 2em; zoom: 1; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt; color: rgb(51, 51, 51); font-variant-caps: normal; font-variant-ligatures: normal; line-height: 24px;">    2. 不能无限制地调用本身，须有个出口，化简为非递归状况处理。</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="word-wrap: break-word; text-indent: 2em; zoom: 1; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 12pt;"><span style="font-size: 12pt; color: rgb(45, 79, 201); font-variant-caps: normal; font-variant-ligatures: normal; line-height: 24px;">由于递归是函数本身一层一层压栈，导致先入栈的不能出栈，空间占满以后就会造成堆栈溢出</span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="word-wrap: break-word; text-indent: 2em; zoom: 1; font-size: 12pt; color: rgb(51, 51, 51); font-weight: bold; line-height: 24px;">    斐波那契数列</span><span style="word-wrap: break-word; text-indent: 2em; zoom: 1; font-size: 12pt; color: rgb(51, 51, 51); line-height: 24px;">指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89........</span></div><div><br clear="none"/></div><div style="font-size: 12pt;"><span style="font-size: 12pt;">        这个数列从第3项开始，每一项都等于前两项之和。</span></div><div><br clear="none" style="font-size: 12pt;"/></div><div><span style="font-size: 12pt; letter-spacing: normal; orphans: 2; text-indent: 28px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-variant-caps: normal; font-variant-ligatures: normal;">        在数学上，斐波纳契数列以如下被以递归</span><span style="font-size: 12pt; letter-spacing: normal; orphans: 2; text-indent: 28px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-variant-caps: normal; font-variant-ligatures: normal;">的方法定义：F(0)=1，F(1)=1, F(n)=F(n-1)+F(n-2)（n&gt;2，n∈N*）</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 18pt;">练习</span></div><div><br clear="none"/></div><div><span style="text-indent: 0pt; font-size: 12pt; color: rgb(1, 1, 1);">1、</span><span style="font-size: 12pt; text-indent: 0pt; color: rgb(1, 1, 1);">利用递归求100的阶乘</span></div><div><span style="text-indent: 0pt; font-size: 12pt; color: rgb(1, 1, 1);">2、<span style="min-height: 16pt; font-size: 12pt; text-indent: 0pt; color: rgb(1, 1, 1);">编写函数计算任意数字的阶乘（两种方法）</span></span></div><div><span style="font-size: 12pt;"><span style="font-size: 12pt;">3、<span style="font-size: 16px; text-indent: 0pt; color: rgb(1, 1, 1);">函数和循环结合打印任意乘法表</span></span></span></div><div><span style="font-size: 12pt;"><span style="font-size: 12pt;"><span style="font-size: 12pt;"><span style="min-height: 16pt; font-size: 12pt; text-indent: 0pt; color: rgb(1, 1, 1);">4、</span>编写函数实现：</span><span style="font-size: 12pt;">两个数平方的阶乘相加的和</span></span></span></div><div><span style="font-size: 12pt;">     </span><span style="font-size: 10pt;">要求：三个函数实现，一个求阶乘，一个求平方，第三个函数利用这两个函数求出最终结果</span></div><div style="min-height: 16pt;"><span style="min-height: 16pt;"><span style="font-size: 12pt;"><span style="font-size: 12pt;"><span style="font-size: 12pt; text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);"><span style="font-size: 12pt; text-indent: 0pt; color: rgb(1, 1, 1);">5、</span>如果一个数恰好等于它的因数（除自身以外）之和，则称该数为“完全数” perfect number。</span></span></span></span></span></div><div style="min-height: 16pt;"><span style="min-height: 16pt; font-size: 12pt; color: rgb(1, 1, 1);">     请列出m~n（m&lt;n）的所有完全数（要求用两个函数实现）</span></div><div style="min-height: 16pt;"><br/></div><div style="min-height: 16pt;"><font style="font-size: 18pt;"><br/></font></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div></div><div><br clear="none"/></div></span>
</div></body></html> 